package com.zhuiYun.project.api.dukaqi.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhuiYun.project.api.dukaqi.IDReader;
import com.zhuiYun.project.api.dukaqi.controller.handler.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
public class CardReaderController {
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    /*
     * @Author wuwan
     * @Description 读取卡片信息
     * @Date 2023/11/10 9:40
     * @Return
     */
    @Scheduled(fixedRate = 1000) // 每秒执行一次
    public void readCard() {
        IDReader idReader = new IDReader();
        String[] args = new String[2];
        args[0] = "6";
        String main = idReader.main(args);
        String customValue = "2f56c0ce-98f0-4a80-bd8e-85dfbec02ee9";
        JSONObject result = new JSONObject();
        if (main != null && main.length() == 10) {
            result.put("customValue", customValue);
            result.put("readResult", main);
            result.put("currentTime", LocalDateTime.now().toString());
            // 将读取到的结果发送给 WebSocket
            try {
                MyWebSocketHandler.sendMessage(result.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
